import {ElNotification} from "element-plus";

/**
 * qq邮箱校验
 */
export function EmailVerify(e){
    const regexEmail = /^[1-9][0-9]{4,10}(?:\.[0-9]+)?@qq\.com$/;
    let email ="";
    let pass =true;

    //QQ邮箱校验
    if (e.includes("@qq.com")){
        if (regexEmail.test(e)) {
            email = e;
        }else {
            if (regexEmail.test(e+'@qq.com')) {
                email =e+"@qq.com";
            }else {
                ElNotification({title: 'Error', message: 'QQ邮箱格式错误', type: 'error'});
                pass=false;
            }
        }
    }else {
        ElNotification({title: 'Error', message: 'QQ邮箱格式错误', type: 'error'});
        return ;
    }
    return {email,pass}
}

/**
 *身份证校验
 */
export function IdCardVerify(e) {
    // 身份证号码正则表达式
    var reg = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
    let pass= reg.test(e);
    let sex = "男"
    if (pass){
        sex = parseInt(e.substr(16, 1)) % 2 === 0 ? '女' : '男';
    }else {
        return {pass,sex}
    }
}


/**
 * 网页参数转对象
 */
export function paramsTransform(e){
    let params = {};
    let pairs = e.split('&');
    for (let i = 0; i < pairs.length; i++) {
        let pair = pairs[i].split('=');
        params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
    }
    return params;
}

/**
 * 判断空值方法
 */
export function isEmpty(value) {
    return value === null || value === undefined || value === '' || value==="" || value.length<=0;
}

/**
 * 分页页码判断转换
 * @param totalNum 总数量
 * @param qureyNum 查询条数
 * @param pageNum 当前第几页
 * @return page 当前第几页
 * @return pageCount 总页数
 */
export function pageSizeNumber(totalNum,qureyNum,pageNum){
    let obj = {page:1, pageCount:1};
    obj.pageCount=Math.ceil((totalNum/qureyNum)>=1? (totalNum/qureyNum) : 1);
    if (pageNum>obj.pageCount){obj.page=obj.pageCount;}
    else {obj.page = pageNum;}
    return obj;
}

/**
 * 分页数据拆分
 * @param list 拆方的数据
 * @param page 当前页
 * @param limit  查询条数
 * @return result 返回最终数据
 */
export function pageListSplit(list,page,limit){
    let starIndex = (page-1)*limit;
    let endIndex = limit*page;
    let result = [];
    list.forEach((item,index)=>{
        if (index>=starIndex&&index<endIndex){
            result.push(item);
        }
    })
    return result;
}


/**
 * GET参数转字符串
 * @param jsonObj
 * @returns {string}
 */
export function jsonToGetParams(jsonObj) {
    const params = Object.entries(jsonObj)
        .map(([key, value]) => `${key}=${value}`)
        .join('&');
    if (params.length >= 1) {
        return "?" + params
    } else {
        return params;
    }
}

/**
 * 请求成功失败提示
 * @param res
 */
export function ms(res){
    if (res.code===200){
        ElNotification({
            title: '提示',
            message: res.msg,
            type: 'success',
        })
    }else {
        ElNotification({
            title: '提示',
            message: res.msg,
            type: 'error',
        })
    }
}
